package org.millioncall.yueyoga.admin.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import org.millioncall.openplatform.common.PaginateParams;
import org.millioncall.openplatform.common.PaginatedList;
import org.millioncall.openplatform.common.StartLimitPairImpl;
import org.millioncall.openplatform.dao.PreparedQuery;
import org.millioncall.yueyoga.admin.dao.BaseDao;
import org.millioncall.yueyoga.admin.dao.MemberDAO;
import org.millioncall.yueyoga.admin.model.AviatorExpression;
import org.millioncall.yueyoga.admin.model.ClassMemberModel;
import org.millioncall.yueyoga.admin.model.Member;
import org.millioncall.yueyoga.admin.model.MemberAdvance;
import org.millioncall.yueyoga.admin.model.MemberModel;
import org.millioncall.yueyoga.admin.service.advancedsearch.AviatorResultHandler;
import org.springframework.stereotype.Repository;

@Repository
public class MemberDAOImpl extends BaseDao implements MemberDAO {

	@Override
	public PaginatedList<Member> findPage(int start, int limit) {
		try {
			PreparedQuery<Member> pq = this.getPagableSqlSessionTemplate()
					.findAll("findMemberPage");
			PaginatedList<Member> pl = pq.list(new PaginateParams(
					new StartLimitPairImpl(start, limit)));
			return pl;
		} catch (RuntimeException re) {
			throw re;
		}
	}

	@Override
	public List<Member> getMemberAll(Map<String, Object> map) {
		try {
			List<Member> pq = this.getPagableSqlSessionTemplate().selectList(
					"findClassMemberAll", map);
			return pq;
		} catch (RuntimeException re) {
			throw re;
		}
	}

	@Override
	public void deleteByIds(Integer[] deids) {
		Map<String, Object> map = new HashMap<String, Object>();
		for (int i = 0; i < deids.length; i++) {
			map.put("id", deids[i]);
			this.getPagableSqlSessionTemplate().insert("deleteMember", map);
		}
	}

	@Override
	public int save(Map<String, Object> map) {
		return this.getPagableSqlSessionTemplate().insert("saveMember", map);
	}

	@Override
	public Member findByCardNumber(Map<String, Object> map) {
		return this.getPagableSqlSessionTemplate().selectOne("findByMap", map);
	}

	@Override
	public Member weixinFindByCardNumber(Map<String, Object> map) {
		return this.getPagableSqlSessionTemplate().selectOne("weixinFindByMap",
				map);
	}

	@Override
	public Member findById(Map<String, Object> map) {
		return this.getPagableSqlSessionTemplate().selectOne("findMemberById",
				map);
	}

	@Override
	public void editMemberEnblue(Integer[] deids, Map<String, Object> map) {
		for (int i = 0; i < deids.length; i++) {
			map.put("memberId", deids[i]);

			this.getPagableSqlSessionTemplate().insert("updateMemberEnable",
					map);
		}
	}

	@Override
	public void editMemberCardNumber(Map<String, Object> map) {
		this.getPagableSqlSessionTemplate().insert("updateMemberCardNumber",
				map);
	}

	@Override
	public List<Member> findMemberByProperty(Map<String, Object> map) {
		List<Member> list = null;
		list = this.getPagableSqlSessionTemplate().selectList(
				"findMemberByProperty", map);
		return list;
	}

	@Override
	public PaginatedList<Member> findPage(int start, int limit,
			Map<String, Object> map) {
		PreparedQuery<Member> pq = this.getPagableSqlSessionTemplate().findAll(
				"findMemberPages", map);
		PaginatedList<Member> pl = pq.list(new PaginateParams(
				new StartLimitPairImpl(start, limit)));
		return pl;
	}

	@Override
	public List<Member> findMemberByCardNumberOrPhone(Object value) {
		List<Member> list = null;
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("value", value);
			list = this.getPagableSqlSessionTemplate().selectList(
					"findMemberByPropertys", map);
		} catch (Exception e) {
			throw e;
		}
		return list;
	}

	@Override
	public int update(Member member) {
		return this.getPagableSqlSessionTemplate().update(
				"updateMemberByPrimaryKeySelective", member);
	}

	@Override
	public int findByMaxId() {
		return this.getPagableSqlSessionTemplate().selectOne(
				"findByMemberMaxId");
	}

	@Override
	public List<Member> findMemberByClsCode(Map<String, Object> map) {
		return this.getPagableSqlSessionTemplate().selectList(
				"findMemberByClsCode", map);
	}

	@Override
	public List<Member> findMemberByGpsId(Map<String, Object> map) {
		return this.getPagableSqlSessionTemplate().selectList(
				"findMemberByGpsId", map);
	}

	@Override
	public Member findByPhone(Map<String, Object> map) {
		return this.getPagableSqlSessionTemplate().selectOne("findByMap", map);
	}

	@Override
	public List<ClassMemberModel> findAllBasicMembers(Map<String, Object> map) {
		return this.getPagableSqlSessionTemplate().selectList(
				"findAllBasicMemberInfo", map);
	}

	@Override
	public PaginatedList<Member> findMemberByLiftClubId(int start, int limit,
			Map<String, Object> map) {
		PreparedQuery<Member> pq = this.getPagableSqlSessionTemplate().findAll(
				"findMemberByLiftClubId", map);
		PaginatedList<Member> pl = pq.list(new PaginateParams(
				new StartLimitPairImpl(start, limit)));
		return pl;
	}

	@Override
	public Member findMemberByMemberInfo(Map<String, Object> params) {
		return this.getPagableSqlSessionTemplate().selectOne(
				"findMemberByMemberInfo", params);
	}

	@Override
	public List<Member> findAllMember(Map<String, Object> map) {
		PreparedQuery<Member> pq = this.getPagableSqlSessionTemplate().findAll(
				"findAllMembers", map);
		return pq.list();
	}

	@Override
	public PaginatedList<Member> findAllMemberByGroup(Integer start,
			Integer limit, Integer groupId) {
		PreparedQuery<Member> pq = this.getPagableSqlSessionTemplate().findAll(
				"findAllMembersGroup", groupId);
		PaginatedList<Member> pl = pq.list(new PaginateParams(
				new StartLimitPairImpl(start, limit)));
		return pl;
	}

	@Override
	public void updateMemberOpenID(Map<String, Object> map) {
		this.getPagableSqlSessionTemplate().update("updateMemberOpenID", map);
	}

	@Override
	public List<Member> findExpireBirthdayMember() {
		PreparedQuery<Member> pq = this.getPagableSqlSessionTemplate().findAll(
				"findExpireBirthdayMember");
		return pq.list();
	}

	@Override
	public List<Member> weixinFindMemberByProperty(String name, String value) {
		List<Member> list = null;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("key", name);
		map.put("value", value);
		list = this.getPagableSqlSessionTemplate().selectList(
				"weixinFindMemberByProperty", map);
		return list;

	}

	@Override
	public List<Member> getAllMembersWithAviator(Map<String, Object> params,
			AviatorExpression expression, Map<String, Object> aviatorParams,
			int limit) {
		AviatorResultHandler<Member> handler = new AviatorResultHandler<Member>(
				expression, aviatorParams, limit);
		this.getPagableSqlSessionTemplate().select("findAdvanceMemberPages",
				params, new RowBounds(0, Integer.MAX_VALUE), handler);
		return handler.getResultList();
	}

	@Override
	public PaginatedList<MemberModel> findFrontMemberList(Integer start,
			Integer limit, Map<String, Object> map) {
		PreparedQuery<MemberModel> pq = this.getPagableSqlSessionTemplate()
				.findAll("findFrontMemberList", map);
		PaginatedList<MemberModel> pl = pq.list(new PaginateParams(
				new StartLimitPairImpl(start, limit)));
		return pl;
	}

	@Override
	public MemberModel findFrontMemberDetailInfo(Map<String, Object> map) {
		return getPagableSqlSessionTemplate().selectOne(
				"findFrontMemberDetailInfo", map);
	}

	@Override
	public List<MemberAdvance> getAllMembersWithAviatorAdvance(
			Map<String, Object> params, AviatorExpression expression,
			Map<String, Object> aviatorParams, int limit) {
		AviatorResultHandler<MemberAdvance> handler = new AviatorResultHandler<MemberAdvance>(
				expression, aviatorParams, limit);
		this.getPagableSqlSessionTemplate().select("findAdvanceMemberPages",
				params, new RowBounds(0, Integer.MAX_VALUE), handler);
		return handler.getResultList();
	}

	@Override
	public List<ClassMemberModel> findClassMember(Map<String, Object> map) {
		List<ClassMemberModel> members = this.getPagableSqlSessionTemplate()
				.selectList("findClassMember", map);
		return members;
	}

	@Override
	public int getTotalMembers() {
		int totalMembers = this.getPagableSqlSessionTemplate().selectOne(
				"findTotalMembers");
		return totalMembers;
	}

	@Override
	public PaginatedList<MemberModel> findFrontPresentMemberList(Integer start,
			Integer limit, Map<String, Object> map) {
		PreparedQuery<MemberModel> pq = this.getPagableSqlSessionTemplate()
				.findAll("findFrontPresentMemberList", map);
		PaginatedList<MemberModel> pl = pq.list(new PaginateParams(
				new StartLimitPairImpl(start, limit)));
		return pl;
	}

	@Override
	public PaginatedList<MemberModel> findFrontAbsentMemberList(Integer start,
			Integer limit, Map<String, Object> map) {
		PreparedQuery<MemberModel> pq = this.getPagableSqlSessionTemplate()
				.findAll("findFrontPresentMemberList", map);
		PaginatedList<MemberModel> pl = pq.list(new PaginateParams(
				new StartLimitPairImpl(start, limit)));
		return pl;
	}

	@Override
	public void updateMemberImg(Map<String, String> params) {
		this.getPagableSqlSessionTemplate().update("updateMemberHeadImg",
				params);

	}

	@Override
	public Member getSimpleMemberById(String memberId) {
		return this.getPagableSqlSessionTemplate().selectOne(
				"findMemberByMemberId", memberId);
	}
}
